package top.heliming.youyou.item.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.heliming.youyou.common.pojo.PageResult;
import top.heliming.youyou.item.pojo.Sku;
import top.heliming.youyou.item.pojo.Spu;
import top.heliming.youyou.item.pojo.SpuBo;
import top.heliming.youyou.item.pojo.SpuDetail;
import top.heliming.youyou.item.service.GoodsService;

/**
 * description: //TODO
 *
 * @author: heliming
 * @date:2019/05/30 下午 11:39
 */
@RestController
public class GoodsController {

  @Autowired
  private GoodsService goodsService;

  /**
   * 分页查询SPU
   * @param page
   * @param rows
   * @param key
   * @return
   */
  @GetMapping("/spu/page")
  public ResponseEntity<PageResult<SpuBo>> querySpuByPage(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "rows", defaultValue = "5") Integer rows,
      @RequestParam(value = "key", required = false) String key) {
    // 分页查询spu信息
    PageResult<SpuBo> result = this.goodsService.querySpuByPageAndSort(page, rows,true, key);
    if (result == null || result.getItems().size() == 0) {
      return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
    return ResponseEntity.ok(result);
  }

  /**
   * 新增商品
   * @param spu
   * @return
   */
  @PostMapping
  public ResponseEntity<Void> saveGoods(@RequestBody SpuBo spu) {
    try {
      this.goodsService.save(spu);
      return new ResponseEntity<>(HttpStatus.CREATED);
    } catch (Exception e) {
      e.printStackTrace();
      return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }

  /**
   * 查询spu详情
   * @param spuId
   * @return
   */
  @GetMapping("spu/detail/{id}")
  public ResponseEntity<SpuDetail> querySpuDetailBySpuId(@PathVariable("id") Long spuId) {
    return ResponseEntity.ok(goodsService.querySpuDetailById(spuId));
  }

  @GetMapping("sku/list")
  public ResponseEntity<List<Sku>> querySkuBySpuId(@RequestParam("id") Long id) {
    List<Sku> skus = this.goodsService.querySkuBySpuId(id);
    if (skus == null || skus.size() == 0) {
      return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
    return ResponseEntity.ok(skus);
  }

  /**
   * 修改商品
   * @param spu
   * @return
   */
  @PutMapping
  public ResponseEntity<Void> updateGoods(@RequestBody SpuBo spu) {
    try {
      this.goodsService.update(spu);
      return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    } catch (Exception e) {
      e.printStackTrace();
      return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }

  @GetMapping("spu/{id}")
  public ResponseEntity<Spu> querySpuById(@PathVariable("id") Long id){
    Spu spu = this.goodsService.querySpuById(id);
    if(spu == null){
      return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
    return ResponseEntity.ok(spu);
  }

  @GetMapping("sku/{id}")
  public ResponseEntity<Sku> querySkuById(@PathVariable("id")Long id){
    Sku sku = this.goodsService.querySkuById(id);
    if (sku == null){
      return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
    return ResponseEntity.ok(sku);
  }
}
